#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename:   features.sh
# Version:    1.0
# Date:       2020/04/22
# Author:     Lz
# Email:      lz843723683@163.com
# History：     
#             Version 1.0, 2020/04/22
# Function:   调用不同版本形态测试程序
# In :        Features testcase name(testcase dir name)
# Out:        
#              0=> TPASS
#              1=> TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------


## TODO : 测试前的初始化验证 
#  In   : $1=>testcase dir name
#  Out  : 
#         0=> TPASS
#         1=> TFAIL
#         other=> TCONF
Init_FT(){
	local testdir="$1"
	# 加载库函数
	local ftlibfile="./lib/ft-lib.sh"
	if [ -f "$ftlibfile" ];then
		source $ftlibfile
	else
		echo "$ftlibfile : Can't found file !"
		return 1
	fi
	
	# 加载常用需求函数库
	local genreqfile="./lib/ft-generalRequest.sh"
	if [ -f "$genreqfile" ];then
		source $genreqfile
	else
		echo "$genreqfile : Can't found file !"
		return 1
	fi

	# 查看是否包含二级目录,以":"作为分隔符
	echo "$testdir" | grep ":" &> /dev/null

	# 判断目录有效性
	if [ $? -ne 0 ];then
	# 不包含二级目录
		if [ ! -d "${testdir}" ];then
			echo "TCONF : Can't find testcases (${testdir})"
			return ${TCONF}
		fi
	else
	# 包含二级目录
		# 读取一级目录
		local testdir_1=$(echo $testdir | cut -d ":" -f 1)
		local testdir_2=$(echo $testdir | cut -d ":" -f 2)
		if [ ! -d "${testdir_1}/${testdir_2}" ];then
			echo "TCONF : Can't find testcases (${testdir_1}/${testdir_2})"
			return ${TCONF}
		fi
	fi
	return ${TPASS}
}


## TODO ：运行当前目录中所有的可执行文件,Run Current Dir File,前提必须进入当前目录
#   In	：
#	  $1 => 目录名
#  Out  ： 
#         0=> TPASS
#         1=> TFAIL
#         other=> TCONF
RunCurDirFile_FT(){
	local testdir="$1"
        # 测试文件路径
        local testfile=""
	local flag="${TPASS}"
	local ret="${TPASS}"
	
        # 测试文件，通过数组保存
        local testfilelist=($(find ./ -maxdepth 1 -type f | sort))

        for testfile in ${testfilelist[*]}
        do
                [[ "$testfile" =~ "readme"|"swp" ]] && continue
                if [ -x "$testfile" ];then
                        bash $testfile
                        flag=$?
                else
			printf "\t\033[1m\033[;33m%s: %s\t[ TCONF ]\033[0m\n\n" "${testdir}/${testfile#*/}" "Permission denied"

			# 禁止降级
			if [ $ret -ne $TFAIL ];then
				ret=${TCONF}
			fi

                        continue
                fi

                if [ $flag -eq ${TPASS} ];then
			printf "\t\033[1m\033[;32m%s\t[ TPASS ]\033[0m\n\n" "${testdir}/${testfile#*/}"
                elif [ $flag -eq ${TFAIL} ];then
			printf "\t\033[1m\033[;31m%s\t[ TFAIL ]\033[0m\n\n" "${testdir}/${testfile#*/}"
			ret=${flag}
                        # 若出错，禁止后续脚本执行
                        # break
                else
			printf "\t\033[1m\033[;33m%s\t[ TCONF ]\033[0m\n\n" "${testdir}/${testfile#*/}"
			
			# 禁止降级
			if [ $ret -ne $TFAIL ];then
				ret=${flag}
			fi
                fi
        done
	
	return $ret
}


## TODO : 执行测试
#  In   : $1=>testcase dir name
#  Out  : 
#         0=> TPASS
#         1=> TFAIL
#         other=> TCONF
Run_FT(){
	# 测试文件目录
	local testdir="$1"
	local ret="${TPASS}"
	local tmpret="${TPASS}"
	
	# 查看是否包含二级目录,以":"作为分隔符
	echo "$testdir" | grep ":" &> /dev/null

	if [ $? -ne 0 ];then
	# 不包含二级目录
		# 进入测试目录
		cd ${testdir}
		RunCurDirFile_FT "${testdir}"
		ret=$?
	else
	# 包含二级目录
		# 读取一级目录
		local testdir_1=$(echo $testdir | cut -d ":" -f 1)
                cd ${testdir_1}
                RunCurDirFile_FT "${testdir_1}"
                ret=$?

		local testdir_2=$(echo $testdir | cut -d ":" -f 2)
		cd ${testdir_2}
		RunCurDirFile_FT "${testdir_1}/${testdir_2}"
		tmpret=$?
	fi

	# 结果过滤
	if [ $ret -eq ${TFAIL} ];then
		return $ret
	elif [ $ret -eq ${TPASS} ];then
		return $tmpret
	else
		if [ $tmpret -eq ${TFAIL} ];then
			return $tmpret
		fi

		return $ret
	fi
}

## TODO	: 解析函数返回值,不为TPASS则退出
#  Exit	:
#         0=> TPASS
#         1=> TFAIL
#         other=> TCONF
RetParse_FT(){
	local ret=$?
	if [ $ret -ne ${TPASS} ];then
		exit $ret
	fi
}


## TODO : Main_FT
#  In	: $1=>testcase dir name
#  Out  : 
#         0=> TPASS
#         1=> TFAIL
#         other=> TCONF
Main_FT(){
	local testdir=$(basename $1)
	cd $(dirname $1)

	Init_FT ${testdir}
	RetParse_FT

	Run_FT ${testdir}
	RetParse_FT

	return ${TPASS}
}

Main_FT $*
exit $?
